package com.dingreading.cloud.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.entity.WhPurchase;
import com.dingreading.cloud.admin.entity.WhPurchaseGoods;
import com.dingreading.cloud.admin.service.GoodsTypeService;
import com.dingreading.cloud.admin.service.WhPurchaseGoodsService;
import com.dingreading.cloud.admin.service.WhPurchaseService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/whPurchaseGoods")
@Api(tags = "总部-仓库采购商品管理")
public class WhPurchaseGoodsController {

    @Resource
    protected WhPurchaseService whPurchaseService;
    @Resource
    protected WhPurchaseGoodsService whPurchaseGoodsService;
    @Resource
    protected GoodsTypeService goodsTypeService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "采购单号/商品名/商品规格名/供应商") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "角色类型 1.仓库") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "状态 0.取消 1.完成 2.保存 3.待入库 4.入库中") @RequestParam(value = "status", required = false) Integer status
    ) {
        PageDto<WhPurchaseGoods> data = this.whPurchaseGoodsService.pageListRealNum(pageUtil, keyword, status, type);
        return R.ok(data);
    }


    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        WhPurchaseGoods data = whPurchaseGoodsService.getById(id);
        if (data == null) return R.fail("采购商品获取失败");
        WhPurchase purchase = whPurchaseService.getByCode(data.getPurchaseCode());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("purchaseGoods", data);
        jsonObject.put("purchase", purchase);
        return R.ok(jsonObject);
    }

}
